package com.guohong.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.guohong.common.RestResult;
import com.guohong.constant.ResponseCodeConstant;
import com.guohong.dto.LoginDto;
import com.guohong.dto.UserPageDto;
import com.guohong.entity.User;
import com.guohong.exception.ServiceException;
import com.guohong.router.VueRouter;
import com.guohong.service.UserService;
import com.guohong.vo.LoginVo;
import com.guohong.vo.UserAddVo;
import com.guohong.vo.UserDeleteVo;
import com.guohong.vo.UserEditVo;
import com.guohong.vo.UserVo;

@RestController
public class UserController implements com.guohong.UserService {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserService userService;
//	@Autowired
//	private UserRoleService userRoleService;

	@RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
	@ResponseBody
	public User selectByPrimaryKey() {
		User user = userService.selectByPrimaryKey(1L);
		return user;
	}

	@Override
	public com.guohong.User getUserById(@PathVariable Long id) {
		logger.debug("---getUserById,id={}", id);
		User user = userService.selectByPrimaryKey(id);

		com.guohong.User userDto = new com.guohong.User();
		BeanUtils.copyProperties(user, userDto);

		return userDto;
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public RestResult<UserPageDto> listUsersByPage(@RequestBody UserVo userVo, HttpServletRequest request, HttpServletResponse response) {
		
		logger.info("--------maodou-user.user.list.accessToken="+request.getHeader("accessToken"));
		
		RestResult<UserPageDto> result = new RestResult<>();
		try {
			UserPageDto userPageDto = userService.listUsersByPage(userVo.getUsername(), userVo.getPageNum(),
					userVo.getPageSize());

			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(userPageDto);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	/*@RequestMapping(value = "/roles/update", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<String> saveOrUpdateUserRole(@RequestBody SaveOrUpdateUserRoleVo saveOrUpdateUserRoleVo) {
		RestResult<String> result = new RestResult<>();

		try {
			String[] roleids = saveOrUpdateUserRoleVo.getRoleIds().split(",");
			List<Long> roles = new ArrayList<Long>();
			for (String roleid : roleids) {
				roles.add(Long.valueOf(roleid));
			}

			userRoleService.saveOrUpdate(saveOrUpdateUserRoleVo.getUserId(), roles);

			result.setCode(ResponseCodeConstant.SUCCESS);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}
		return result;
	}*/
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<LoginDto> login(@RequestBody LoginVo loginVo) {
		RestResult<LoginDto> result = new RestResult<>();

		try {
			LoginDto loginDto = userService.login(loginVo.getUsername(), loginVo.getPassword());

			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(loginDto);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}

	@RequestMapping(value = "/routers/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public RestResult<List<VueRouter>> getUserRouters(@PathVariable(value = "userId") Long userId) {
		RestResult<List<VueRouter>> result = new RestResult<>();
		try {
			List<VueRouter> routers = userService.getVueRouters(userId);

			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(routers);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<Integer> userAdd(@RequestBody UserAddVo addUserVo, HttpServletRequest request, HttpServletResponse response) {
		RestResult<Integer> result = new RestResult<>();

		try {
			logger.info("--------maodou-user.user.list.accessToken="+request.getHeader("accessToken"));
			String token = request.getHeader("accessToken");
			addUserVo.setToken(token);
			
			Integer count = userService.insert(addUserVo);
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(count);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<Integer> userEdit(@RequestBody UserEditVo userEditVo) {
		RestResult<Integer> result = new RestResult<>();

		try {
			Integer count = userService.update(userEditVo);
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(count);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<String> userDelete(@RequestBody UserDeleteVo userDeleteVo) {
		RestResult<String> result = new RestResult<>();

		try {
			userService.deleteUsers(userDeleteVo);
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData("");
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}
		return result;
	}
}



